package utils
{
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;

	public class ColorTool
	{
		private static var Brightness_Matrix:Array = [1, 0, 0, 0, 50, 0, 1, 0, 0, 50, 0, 0, 1, 0, 50, 0, 0, 0, 1, 0];
		private static var deColorTrans:ColorTransform = new ColorTransform(0.2, 0.2, 0.2, 0.9, 100, 100, 100, 0);
		private static var suColorTrans:ColorTransform = new ColorTransform(1, 1, 1, 1, 0, 0, 0, 0);
		
		private static var butLightColorTrans:ColorTransform = new ColorTransform(1,1,1,1,90,0,0,0);
		private static var butLightYellowTrans:ColorTransform = new ColorTransform(1,.56,0,1,255,142,0,0);
		
		private static var blackTrans:ColorTransform = new ColorTransform(0,0,0,1,0,0,0,0);
		
		private static var yellowTrans:ColorTransform = new ColorTransform(1,1,1,1,229,153,0,0);
		private static var redTrans:ColorTransform = new ColorTransform(0.78,1,-1,1,142,25,-255,0);
		
		private static var shadowFilter:DropShadowFilter = new DropShadowFilter(6,45,0,.7);
		private static var shadowFilter2:DropShadowFilter = new DropShadowFilter(3,45,0,.7,2,2,2);
		
		private static var green:GlowFilter = new GlowFilter(0x00CC66,1,6,6,5,1);
		private static var gold:GlowFilter = new GlowFilter(0xFF6600,1,6,6,3);
		
		private static var glowBlack:GlowFilter = new GlowFilter(0,1,2,2,15);
		
		public static function addShadow(target:DisplayObject):void
		{
			//target.filters  = target.filters;
			target.filters.push(shadowFilter);
			target.filters  = target.filters;
		}
		
		public static function glowGreen(target:DisplayObject):void
		{
			target.filters = [green,shadowFilter2];
		}
		
		public static function glowGold(target:DisplayObject):void
		{
			target.filters = [gold,shadowFilter2];
		}
		
		public static function glowRing(target:DisplayObject):void
		{
			target.filters = [glowBlack];	
		}
		
		public static function lightButtonYellow(target:DisplayObject):void
		{
			target.transform.colorTransform = butLightYellowTrans;
		}
		
		public static function lightButton(target:DisplayObject):void
		{
			target.transform.colorTransform = butLightColorTrans;
		}
		
		public static function addBrightness(target:DisplayObject,level:int = 50):void
		{
			Brightness_Matrix[4] = level;
            Brightness_Matrix[9] = level;
            Brightness_Matrix[14] = level;
            target.filters = [new ColorMatrixFilter(Brightness_Matrix)];
		}
		
		public static function yellow(target:DisplayObject):void
		{
			target.transform.colorTransform = yellowTrans;
		}
		
		public static function red(target:DisplayObject):void
		{
			target.transform.colorTransform = redTrans;
		}
		
		public static function desaturation(target:DisplayObject) : void
        {
            target.transform.colorTransform = deColorTrans;
        }
        
        public static function saturation(target:DisplayObject):void
        {
        	target.transform.colorTransform = suColorTrans;
        }
        
        public static function black(target:DisplayObject):void
        {
        	target.transform.colorTransform = blackTrans;
        }
        
        public static function dropShadow(target:DisplayObject):void
        {
        	target.filters = [shadowFilter];
        }
		
		public static function drawPanelBg(target:Graphics,bgWidth:int,bgHeight:int,bgAlpha:Number = 1,bgTopColor:uint = 0x666666,bgBottomColor:uint = 0x111111):void
		{
			var fillType:String = GradientType.LINEAR;
			var colors:Array = [0xB7B7B7, 0x333333];
			var alphas:Array = [bgAlpha, bgAlpha];
			var ratios:Array = [0x00, 0xFF];
			var matr:Matrix = new Matrix();
			matr.createGradientBox(bgWidth, bgHeight, Math.PI / 2, 0, 0);
			
			with (target)
			{
				clear();
				beginGradientFill(fillType,colors,alphas,ratios,matr);
				drawRoundRect(0,0,bgWidth,bgHeight,13,13);
				endFill();
				colors = [bgTopColor,bgBottomColor];
				matr.createGradientBox(bgWidth - 6,bgHeight - 6,Math.PI / 2,0,0);
				beginGradientFill(fillType,colors,alphas,ratios,matr);
				drawRoundRect(3,3,bgWidth - 6, bgHeight - 6,13,13);
				endFill();
			}
		}
		
		public static function drawHallbg(target:Graphics,bgWidth:int,bgHeight:int,bgAlpha:Number = .8):void
		{
			var fillType:String = GradientType.RADIAL;
			var colors:Array = [0xFF0000, 0x111111];
			var alphas:Array = [bgAlpha, bgAlpha];
			var ratios:Array = [0x00, 0xFF];
			var matr:Matrix = new Matrix();
			matr.createGradientBox(bgWidth, bgHeight, Math.PI / 2, 0, 0);
			//
			matr.scale(1.2,1.2);
			matr.translate(-140,-180);
			with (target)
			{
				clear();
				beginGradientFill(fillType,colors,alphas,ratios,matr);
				drawRoundRect(0,0,bgWidth,bgHeight,5,5);
				endFill();
				/*colors = [0x666666,0x111111];
				matr.createGradientBox(bgWidth - 6,bgHeight - 6,Math.PI / 2,0,0);
				beginGradientFill(fillType,colors,alphas,ratios,matr);
				drawRoundRect(3,3,bgWidth - 6, bgHeight - 6,5,5);
				endFill();*/
			}
		}
	}
}